import {deleteTag} from '~/server/database/repositories/tag'
import {deleteItemsByTagId} from '~/server/database/repositories/itemOperation'
export default defineEventHandler(async(e)=>{
  const id = e.context.params?.id ? parseInt(e.context.params.id) : undefined
  if (!id)
    return sendError(e, createError({ statusCode: 400, statusMessage: '参数错误' }))
  try {
    const count = await deleteItemsByTagId(id)
    await deleteTag(id)
    if(count>0){
      return {
        resource:{},
        msg:'已删除标签和对应账目！'
      }
    }else{
      return {
        resource:{},
        msg:'已删除标签！'
      }
    }
  }
  catch (error) {
    return sendError(e, createError({ statusCode: 500, statusMessage: '服务器错误' }))
  }
})